package com;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YJ
 * Date: 2024-3-31
 * Time: 20:23
 */
public class compareExer {
    public static void main(String[] args) {
        System.out.println(compare("2.0.1", "2"));//1
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * <p>
     * 比较版本号
     *
     * @param version1 string字符串
     * @param version2 string字符串
     * @return int整型
     */
    public static int compare(String version1, String version2) {
        // write code here
        int s1 = version1.length();
        int s2 = version2.length();
        int i = 0;
        int j = 0;
        while (i < s1 || j < s2) {
            long v1 = 0;
            while (i < s1 && version1.charAt(i) != '.') {
                v1 = v1 * 10 + (version1.charAt(i) - '0');
                i++;
            }
            //跳过‘.’
            i++;
            long v2 = 0;
            while (j < s2 && version2.charAt(j) != '.') {
                v2 = v2 * 10 + (version2.charAt(j) - '0');
                j++;
            }
            //跳过‘.’
            j++;
            //比较两个数的大小
            if (v1 > v2) return 1;
            if (v1 < v2) return -1;
        }
        return 0;
    }
}
